

let sqlite3 = require('sqlite3').verbose();

let dbUtils = {

    db:null,

    init(dbfile){
        console.log("------db init called!");
        this.db = new sqlite3.Database(dbfile);
    },

    close(){
        if(this.db){
            console.log("------db close called!");
            this.db.close();
        }
    },

    get(sql){
        let self = this;
        return new Promise((resolve,reject) => {
            self.db.get(sql,(err,result)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(result);
                }
            });
        });
    },

    all(sql){
        let self = this;
        return new Promise((resolve, reject) => {
            self.db.all(sql,(err,results)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(results);
                }
            });
        })
    },

    run(sql){
        let self = this;
        return new Promise((resolve, reject) => {
           self.db.run(sql,(err,result)=>{
               if(err){
                   reject(err);
               }else{
                   resolve(result);
               }
           })
        });
    },
};
module.exports = dbUtils;
